//
//  CRMCustomerHeaderView.m
//  New_Runway
//
//  Created by NewLife on 2018/2/26.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CRMCustomerHeaderView.h"

#import "NRWCarCustomerIntentModel.h"

@implementation CRMCustomerHeaderView
{
//    UIImageView *iconImageView;
    UILabel *mobileLabel;
    UIButton *callBtn;
//    UIButton *chatBtn;
    UIView *lineView1;
    
    UILabel *budgetLabel;
    UILabel *fromLabel;
    UILabel *locationLabel;
    UILabel *levelLabel;
    
    UIView *lineView2;
    
    UILabel *remarkLabel;
}

- (void)setModel:(NRWCarCustomerIntentModel *)model {
    
    _model = model;
    
    mobileLabel.text = model.result.customerPhone;
    
    NSString *budgetStr = [NSString stringWithFormat:@"预算: %.2f-%.2f万", model.result.customerBudgetMin, model.result.customerBudgetMax];
    budgetLabel.attributedText = [budgetStr attributedStringWithFirstTextColor:getColorFromString(@"989898") firstRange:NSMakeRange(0, 3) secondTextColor:getColorFromString(@"323232") secondRange:NSMakeRange(3, budgetStr.length - 3)];
    
    NSString *fromStr = [NSString stringWithFormat:@"客户来源: %@", isEmptyOrNilString(model.result.channelName)? @"": model.result.channelName];
    fromLabel.attributedText = [fromStr attributedStringWithFirstTextColor:getColorFromString(@"989898") firstRange:NSMakeRange(0, 5) secondTextColor:getColorFromString(@"323232") secondRange:NSMakeRange(5, fromStr.length - 5)];
    
    NSString *locationStr = [NSString stringWithFormat:@"归属地区: %@ %@", [isEmptyOrNilString(model.result.provinceName)? @"": model.result.provinceName stringByReplacingOccurrencesOfString:@"省" withString:@""], [isEmptyOrNilString(model.result.cityName)? @"": model.result.cityName stringByReplacingOccurrencesOfString:@"市" withString:@""]];
    locationLabel.attributedText = [locationStr attributedStringWithFirstTextColor:getColorFromString(@"989898") firstRange:NSMakeRange(0, 5) secondTextColor:getColorFromString(@"323232") secondRange:NSMakeRange(5, locationStr.length - 5)];
    
    NSString *levelStr = [NSString stringWithFormat:@"客户级别: %@", isEmptyOrNilString(model.result.levelTxt)? @"": model.result.levelTxt];
    levelLabel.attributedText = [levelStr attributedStringWithFirstTextColor:getColorFromString(@"989898") firstRange:NSMakeRange(0, 5) secondTextColor:getColorFromString(@"323232") secondRange:NSMakeRange(5, levelStr.length - 5)];
    
    NSString *remarkStr = [NSString stringWithFormat:@"备注: %@", isEmptyOrNilString(model.result.remark)? @"": model.result.remark];
    remarkLabel.attributedText = [remarkStr attributedStringWithFirstTextColor:getColorFromString(@"989898") firstRange:NSMakeRange(0, 3) secondTextColor:getColorFromString(@"323232") secondRange:NSMakeRange(3, remarkStr.length - 3)];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupUI];
        
        [self layoutUI];
        
    }
    return self;
}

- (void)setupUI {
    
//    iconImageView = [[UIImageView alloc] init];
//    iconImageView.backgroundColor = [UIColor whiteColor];
//    iconImageView.opaque = YES;
//    iconImageView.layer.cornerRadius = 15.5;
//    iconImageView.layer.masksToBounds = YES;
    
    mobileLabel = [[UILabel alloc] init];
    mobileLabel.textColor = [UIColor colorFromHexCode:@"323232"];
    mobileLabel.font = getPingfangFont(14);
    mobileLabel.backgroundColor = [UIColor whiteColor];
    mobileLabel.opaque = YES;
    
    callBtn = [[UIButton alloc] init];
    [callBtn setImage:[UIImage imageNamed:@"crm-call"] forState:UIControlStateNormal];
    callBtn.backgroundColor = [UIColor whiteColor];
    callBtn.opaque = YES;
    [callBtn addTarget:self action:@selector(callBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    
//    chatBtn = [[UIButton alloc] init];
//    [chatBtn setImage:[UIImage imageNamed:@"crm-chat"] forState:UIControlStateNormal];
//    chatBtn.backgroundColor = [UIColor whiteColor];
//    chatBtn.opaque = YES;
//    [chatBtn addTarget:self action:@selector(chatBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    
    lineView1 = [[UIView alloc] init];
    lineView1.backgroundColor = getColorFromString(@"e1e1e1");
    lineView1.opaque = YES;
    
    budgetLabel = [[UILabel alloc] init];
    budgetLabel.textColor = getColorFromString(@"989898");
    budgetLabel.font = getPingfangFont(13);
    budgetLabel.backgroundColor = [UIColor whiteColor];
    budgetLabel.opaque = YES;
    
    fromLabel = [[UILabel alloc] init];
    fromLabel.textColor = getColorFromString(@"989898");
    fromLabel.font = getPingfangFont(13);
    fromLabel.backgroundColor = [UIColor whiteColor];
    fromLabel.opaque = YES;
    
    locationLabel = [[UILabel alloc] init];
    locationLabel.textColor = getColorFromString(@"989898");
    locationLabel.font = getPingfangFont(13);
    locationLabel.backgroundColor = [UIColor whiteColor];
    locationLabel.opaque = YES;
    
    levelLabel = [[UILabel alloc] init];
    levelLabel.textColor = getColorFromString(@"989898");
    levelLabel.font = getPingfangFont(13);
    levelLabel.backgroundColor = [UIColor whiteColor];
    levelLabel.opaque = YES;
    
    lineView2 = [[UIView alloc] init];
    lineView2.backgroundColor = getColorFromString(@"e1e1e1");
    lineView2.opaque = YES;
    
    remarkLabel = [[UILabel alloc] init];
    remarkLabel.textColor = getColorFromString(@"989898");
    remarkLabel.font = getPingfangFont(13);
    remarkLabel.backgroundColor = [UIColor whiteColor];
    remarkLabel.opaque = YES;
    remarkLabel.numberOfLines = 2;
    
//    [self addSubview:iconImageView];
    [self addSubview:mobileLabel];
    [self addSubview:callBtn];
//    [self addSubview:chatBtn];
    [self addSubview:lineView1];
    
    [self addSubview:budgetLabel];
    [self addSubview:fromLabel];
    [self addSubview:locationLabel];
    [self addSubview:levelLabel];
    
    [self addSubview:lineView2];
    [self addSubview:remarkLabel];
    
    budgetLabel.text = @"预算:";
    fromLabel.text = @"客户来源";
    locationLabel.text = @"归属地区:";
    levelLabel.text = @"客户等级:";
    remarkLabel.text = @"备注:";
}

- (void)layoutUI {
    
    [mobileLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(17);
        make.centerY.equalTo(self.mas_top).offset(22.5);
    }];
    
    [callBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-17);
        make.centerY.equalTo(mobileLabel.mas_centerY);
    }];
    
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset((45));
        make.left.equalTo(mobileLabel.mas_left);
        make.right.equalTo(self.mas_right);
        make.height.equalTo(@0.5);
    }];
    
    [budgetLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(mobileLabel.mas_left);
        make.top.equalTo(lineView1.mas_bottom).offset((13.5));
        make.right.lessThanOrEqualTo(self.mas_centerX);
    }];
    
    [fromLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_centerX);
        make.centerY.equalTo(budgetLabel.mas_centerY);
        make.right.lessThanOrEqualTo(self.mas_right).offset(-17);
    }];
    
    [locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(budgetLabel.mas_left);
        make.top.equalTo(budgetLabel.mas_bottom).offset((14.5));
        make.right.equalTo(levelLabel.mas_left).offset(-(7));
        make.right.lessThanOrEqualTo(self.mas_centerX);
    }];
    
    [levelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_centerX);
        make.top.equalTo(locationLabel.mas_top);
        make.right.lessThanOrEqualTo(self.mas_right).offset(-17);
    }];
    
    [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(levelLabel.mas_bottom).offset((13));
        make.left.right.equalTo(self);
        make.height.equalTo(@0.5);
    }];
    
    [remarkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView2.mas_bottom).offset((14));
        make.left.equalTo(self.mas_left).offset((17));
        make.right.equalTo(self.mas_right).offset(-(30));
    }];
}

- (void)callBtnDidClick:(UIButton *)sender {
    
    if ([self.delegate respondsToSelector:@selector(customerHeaderView:callBtnDidClick:)]) {
        
        [self.delegate customerHeaderView:self callBtnDidClick:sender];
    }
}

//- (void)chatBtnDidClick:(UIButton *)sender {
//    
//    
//}

@end
